package com.lijunyan.blackmusic.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/*
 * path: RestaurantDemo
 * Author: 86133
 * Create: 2019/12/3 11:35
 * Changes (from 2019/12/3)
 * Desc:
 */
public class DBOpenHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "DataInfo.db";
    private static final int DB_VERSION = 1;
    private static DBOpenHelper dbOpenHelper;

    public static final String TABLE_USER_INFO_NAME = "user_info";//用户注册信息
    public static final String TABLE_TABLE_INFO_NAME = "table_info";//台桌信息
    public static final String TABLE_USER_ORDER_NAME = "user_order";//用户订单信息

    private static final String SQL_CREATE_USER_INFO = "CREATE TABLE " + TABLE_USER_INFO_NAME + " (userName TEXT,userPassword TEXT,hadRead TEXT,currentTimeMillis TEXT);";

    private static final String SQL_CREATE_TABLE_INFO = "CREATE TABLE " + TABLE_TABLE_INFO_NAME + " (tableNum TEXT,tableStatus TEXT,allPeopleNum TEXT,orderPeopleNum TEXT,comingPeopleNum TEXT,hadRead TEXT,currentTimeMillis TEXT);";

    private static final String SQL_CREATE_USER_ORDER = "CREATE TABLE " + TABLE_USER_ORDER_NAME + " (tableNum TEXT,foodName TEXT,foodNum TEXT,foodPic TEXT,foodStatus TEXT,hadRead TEXT,currentTimeMillis TEXT);";


    private DBOpenHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        // TODO Auto-generated constructor stub
    }

    public static synchronized DBOpenHelper getinstance(Context context) {
        if (dbOpenHelper == null) {
            dbOpenHelper = new DBOpenHelper(context);
        }
        return dbOpenHelper;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        int initDBVersion = 1;
        initDB(db);
        onUpgrade(db, initDBVersion, DB_VERSION);
//        Log.i(TAG, "重新创建数据表");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        for (int i = oldVersion; i < newVersion; i++) {
            switch (i) {
                case 1:
                    upToDbVersion2(db);
                    break;
                case 2:
                    upToDbVersion3(db);
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * 初始化数据库
     * @param database SQLiteDatabase
     */
    public static void initDB(SQLiteDatabase database) {
        database.execSQL(SQL_CREATE_USER_INFO);
        database.execSQL(SQL_CREATE_TABLE_INFO);
        database.execSQL(SQL_CREATE_USER_ORDER);
    }

    /**
     * 数据库升级到2的改变
     * @param database SQLiteDatabase
     * test:2.0:
     *    时想在阅读记录原有表中新加一列
     */
    public static void upToDbVersion2(SQLiteDatabase database) {
        String updateSql = "alter table " + TABLE_USER_INFO_NAME + " add column store varchar(5)";
        database.execSQL(updateSql);
    }

    /**
     * 数据库升级到3的改变
     * @param database SQLiteDatabase
     * test:升级到3.0:
     *  想把阅读记录表中的store列中的值全改成100：
     *  在数据库中新增一个阅读记录的表
     */
    public static void upToDbVersion3(SQLiteDatabase database) {
        ContentValues values = new ContentValues();
        database.update(TABLE_USER_INFO_NAME, values, null, null);
//        database.execSQL(SQL_CREATE_BBS_PUBLISH_POST);//创建发布帖子的草稿箱数据库
    }
}
